<template>
	<view class="myexpert-page">
		<u-navbar :autoBack="true" :placeholder="true" title="我的发布" bgColor="rgba(255,255,255,0)"></u-navbar>
		<view class="list-box" v-if="info.nickname">
			<view class="list-item">
				<view class="switch">
					<view class="switch-left">
						<text>{{info.open_start}} - {{info.open_end}}</text>
						<!-- <u-icon v-if="value == true" @click="openTimePicker" name="edit-pen" color="#000000"
							size="24"></u-icon> -->
					</view>
					<view style="display: flex;align-items: center;">
						<view class="switch-right">
							<view class="text" v-if="value == false">不接:</view>
							<view class="text" v-else>接单:</view>
							<u-switch size="20" style="margin-left: 10rpx;" active-color="#5755F2"
								inactive-color="#B6B6B6" v-model="value" @change="change"></u-switch>
						</view>
					</view>
				</view>
				<view class="below">
					<view class="below-box">
						<image class="head" :src="'https://m.lqvvvv.com'+info.avatar" mode=""></image>
						<view class="head-info">
							<view class="nickname">
								{{info.nickname}}
							</view>
							<view class="head_id">
								id:{{info.user_id}}
							</view>
						</view>
					</view>
					<view class="switch-box" v-if="value == true">
						<view class="text" @click="change2(true)" :class="value2 == true?'textchek':''">在线</view>
						<view class="text" @click="change2(false)" :class="value2 == false?'textchek':''">离线</view>
					</view>
				</view>
				<view class="info-btn">
					<view v-if="userinfo.friend_level == 0" @click="gonavto">
						<img src="https://m.lqvvvv.com/images/vip1.png" alt="" />
					</view>
					<view v-if="userinfo.friend_level == 1" @click="gonavto">
						<img src="https://m.lqvvvv.com/images/vip2.png" alt="" />
					</view>
					<view v-if="userinfo.friend_level == 2" @click="gonavto">
						<img src="https://m.lqvvvv.com/images/vip3.png" alt="" />
					</view>
					<view v-if="userinfo.friend_level == 3" @click="gonavto">
						<img src="https://m.lqvvvv.com/images/vip4.png" alt="" />
					</view>
					<view class="btn">
						<u-button type="primary" color="#4A66F8"
							@click="$navTo.togo('/pages/my/expert/myaccept/project/index')" shape="circle"
							text="选择项目"></u-button>
					</view>
				</view>
			</view>
		</view>
		<!-- 时间选择器 -->
		<u-datetime-picker :minDate="minDate" :formatter="formatter" :show="showPicker" title="开始时间"
			@cancel="showPicker = false" @confirm="confirm" mode="datetime"></u-datetime-picker>
		<u-datetime-picker :minDate="minDate2" :formatter="formatter" :maxDate="maxDate2" :show="showPicker2"
			title="结束时间" @cancel="showPicker2 = false" @confirm="confirm2" mode="datetime"></u-datetime-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: true,
				info: {},
				value2: false,
				userinfo: {},
				showPicker: false, // 控制时间选择器的显示
				showPicker2: false, // 控制时间选择器的显示
				timevalue1: '',
				timevalue2: '',
				minDate: new Date().getTime(),
				minDate2: new Date().getTime(),
				maxDate2: new Date(new Date().getTime() + 2 * 24 * 60 * 60 * 1000).getTime(),
			}
		},
		onLoad() {
			this.postlist()
			this.postuserinfo()
		},
		methods: {
			change2(e) {
				console.log(e)
				if (e == true) {
					this.openTimePicker()
				} else {
					uni.showLoading({
						mask: true
					})
					let params = {
						is_show: 0
					}
					this.$Request({
						method: 'POST',
						url: '/api/friend/updateInfo',
						data: params,
						ismsg: true,
					}).then(res => {
						uni.hideLoading()
						if (res.code == 1) {
							this.postlist()
						}
					})
				}
			},
			gonavto() {
				uni.navigateTo({
					url: '/pages/my/sage/index'
				})
			},
			confirm(e) {
				this.timevalue1 = e.value
				this.minDate2 = e.value
				this.maxDate2 = new Date(e.value + 2 * 24 * 60 * 60 * 1000).getTime()
				this.showPicker = false
				this.showPicker2 = true
			},
			confirm2(e) {
				this.timevalue2 = e.value
				this.showPicker2 = false
				uni.showLoading({
					mask: true
				})
				this.$Request({
					method: 'POST',
					url: '/api/friend/updateInfo',
					data: {
						is_show: 1
					},
					ismsg: true,
				}).then(res => {
					if (res.code == 1) {
						let params = {
							open_start: uni.$u.timeFormat(this.timevalue1, 'yyyy-mm-dd hh:MM:ss'),
							open_end: uni.$u.timeFormat(this.timevalue2, 'yyyy-mm-dd hh:MM:ss')
						}
						this.$Request({
							method: 'POST',
							url: '/api/friend/updateInfo',
							data: params,
							ismsg: true,
						}).then(res => {
							uni.hideLoading()
							if (res.code == 1) {
								this.postlist()
							}
						})
					}
				})
			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`;
				}
				if (type === 'month') {
					return `${value}月`;
				}
				if (type === 'day') {
					return `${value}日`;
				}
				if (type === 'hour') {
					return `${value}时`;
				}
				if (type === 'minute') {
					return `${value}分`;
				}

				return value;
			},
			openTimePicker() {
				this.showPicker2 = true; // 显示时间选择器
			},
			postlist() {
				let params = {}
				this.$Request({
					method: 'POST',
					url: '/api/friend/getMyInfo',
					data: params,
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						this.info = res.data
						if (this.info.is_show == 1) {
							this.value2 = true
						} else {
							this.value2 = false
						}
						if (this.info.is_open == 1) {
							this.value = true
						} else {
							this.value = false
						}
					} else {
						this.info = {}
					}
				})
			},
			postuserinfo() {
				let params = {}
				this.$Request({
					method: 'POST',
					url: '/api/user/getMyInfo',
					data: params,
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						this.userinfo = res.data
					} else {
						this.userinfo = {}
					}
				})
			},
			change(e) {
				console.log(e)
				uni.showLoading({
					mask: true
				})
				let params = {
					is_open: ''
				}
				if (e == true) {
					params.is_open = 1
				} else {
					params.is_open = 0
				}
				this.$Request({
					method: 'POST',
					url: '/api/friend/updateInfo',
					data: params,
					ismsg: true,
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						this.postlist()
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.myexpert-page {
		min-height: 100vh;
		background-image: url('https://m.lqvvvv.com/assets/static/index/nav-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 800rpx;
		background-color: #F7F8FC;

		.list-box {
			padding: 20rpx 30rpx;
			margin-top: 30rpx;

			.list-item {
				background: #FFFFFF;
				border-radius: 26rpx;
				padding: 20rpx;

				.switch {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.switch-left {
						display: flex;
						align-items: center;
						font-size: 24rpx;

						text {
							margin-right: 10rpx;
						}
					}

					.switch-right {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.text {
							font-size: 24rpx;
						}
					}


				}

				.below {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 20rpx;

					.below-box {
						display: flex;
						align-items: center;
					}

					.switch-box {
						display: flex;
						align-items: center;
						border-radius: 34rpx 34rpx 34rpx 34rpx;
						border: 1rpx solid #C9C9FF;
						padding: 10rpx;

						.text {
							font-size: 26rpx;
							padding: 6rpx 20rpx;
						}

						.textchek {
							background: #E1E1FF;
							border-radius: 24rpx 24rpx 24rpx 24rpx;
							color: #5755F2;

						}
					}

					.head {
						width: 100rpx;
						height: 100rpx;
						border-radius: 50%;
					}

					.head-info {
						margin-left: 20rpx;

						.nickname {
							font-size: 26rpx;
							color: #333333;
							font-weight: 700;
						}

						.head_id {
							margin-top: 5rpx;
							font-size: 24rpx;
							color: #999999;
						}
					}
				}

				.info-btn {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 20rpx;

					.btn {

						.u-button {
							width: 140rpx;
							height: 60rpx;
						}
					}

					img {
						width: 176rpx;
						height: 60rpx;
					}
				}
			}
		}
	}
</style>